package com.braveman.im.ui.fragment;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;

import com.braveman.im.R;
import com.braveman.im.bean.FriendshipListBean;
import com.braveman.im.bean.GetMineFriendsBean;
import com.braveman.im.manager.ApiManager;
import com.braveman.im.ui.activity.message.ChatAllActivity;
import com.braveman.im.ui.adapter.MineFriendsAdapter;
import com.braveman.im.ui.weiget.SlideFromBottomPopup;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ht.baselibs.retrofit.NetCallBack;
import com.ht.baselibs.ui.base.BaseFragment;
import com.ht.baselibs.utils.ActivityUtils;

import java.util.ArrayList;
import java.util.List;

import ezy.ui.layout.LoadingLayout;
import retrofit2.Call;
import retrofit2.Response;

public class FriendsFragment extends BaseFragment {
    private MineFriendsAdapter mineFriendsAdapter;
    private List<FriendshipListBean> friendshipListBeanList = new ArrayList<>();//展示数据
    private List<GetMineFriendsBean> friendTitleList = new ArrayList<>();
    private List<FriendshipListBean> allFriendList = new ArrayList<>();//所有好友
    private RecyclerView mLvActivityListMember;
    private LoadingLayout loadingLayout;
    private List<GetMineFriendsBean> list;

    @Override
    protected boolean isStaticPage() {
        return true;
    }

    @Override
    protected View initContentView() {
        return LayoutInflater.from(mContext).inflate(R.layout.frag_friends_fragment, null);
    }

    @Override
    protected void initChildView() {
        mineFriendsAdapter = new MineFriendsAdapter(R.layout.em_row_friends, R.layout.item_friends_list_head, friendshipListBeanList);
        loadingLayout = findViewById(R.id.loading_layout);
        mLvActivityListMember = findViewById(R.id.lv_Activity_list_member);
        mLvActivityListMember.setLayoutManager(new LinearLayoutManager(mContext));
        mLvActivityListMember.setAdapter(mineFriendsAdapter);
    }

    @Override
    protected void initArgumentsData(Bundle args) {
        super.initArgumentsData(args);
    }

    @Override
    protected void load() {

        ApiManager.getApi().getMineFriends().enqueue(new NetCallBack<List<GetMineFriendsBean>>() {
            @Override
            protected void onSuccess(Call<List<GetMineFriendsBean>> call, Response<List<GetMineFriendsBean>> response) {

                if (response != null) {
                    if (friendshipListBeanList != null) {
                        friendshipListBeanList.clear();
                    }
                    list = new ArrayList<>();
                    list.addAll(response.body());
                    String name = "";
                    for (GetMineFriendsBean getMineFriendsBean : list) {
                        allFriendList.addAll(getMineFriendsBean.friendshipList);
                        if (!name.equals(getMineFriendsBean.name)) {
                            FriendshipListBean friendshipListBean = new FriendshipListBean(true, getMineFriendsBean.name);
                            friendshipListBeanList.add(friendshipListBean);
                            if (allFriendList != null) {

                                if ("我的好友".equals(getMineFriendsBean.name)) {
                                    friendshipListBeanList.addAll(allFriendList);
                                }
                            }
                        }
                        name = getMineFriendsBean.name;
                    }


                    if (friendshipListBeanList.size() == 0) {
                        loadingLayout.showEmpty();
                    } else {
                        loadingLayout.showContent();
                    }
                } else {

                    loadingLayout.showError();
                }


                if (mineFriendsAdapter != null) {
                    mineFriendsAdapter.notifyDataSetChanged();
                }

            }
        });
    }

    @Override
    protected void setChildViewListener() {
        mineFriendsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                if (friendshipListBeanList.get(position).isHeader) {
                    String positinName = friendshipListBeanList.get(position).header;
                    friendshipListBeanList.clear();
                    String name = "";
                    for (GetMineFriendsBean friends : list) {

                        if (!name.equals(friends.name)) {
                            FriendshipListBean friendshipListBean = new FriendshipListBean(true, friends.name);
                            friendshipListBeanList.add(friendshipListBean);
                        }
                        if (positinName.equals(friends.name)) {
                            friendshipListBeanList.addAll(friends.friendshipList);
                        }
                        name = friends.name;
                    }
                } else {
                    Bundle bundle = new Bundle();
                    bundle.putInt("states", 3);
                    bundle.putString("usrName", friendshipListBeanList.get(position).friendName);
                    bundle.putString("user_id", friendshipListBeanList.get(position).friendImUsername);
                    bundle.putString("imFriendsId", friendshipListBeanList.get(position).friendImUsername);
                    ActivityUtils.startActivity(mContext, ChatAllActivity.class, bundle);
                }
                if (mineFriendsAdapter != null) {
                    mineFriendsAdapter.notifyDataSetChanged();
                }
            }
        });

        mineFriendsAdapter.setOnItemLongClickListener(new BaseQuickAdapter.OnItemLongClickListener() {
            @Override
            public boolean onItemLongClick(BaseQuickAdapter adapter, View view, int position) {
                if (friendshipListBeanList.get(position).isHeader) {
                    SlideFromBottomPopup slideFromBottomPopup = new SlideFromBottomPopup(mContext);
                    slideFromBottomPopup.showPopupWindow();
                }
                return true;
            }
        });
    }


}
